/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "secure_element_seservice.h"
#include "secure_element_common.h"

namespace OHOS {
namespace SE {
namespace OMAPI {
SEServiceImpl::SEServiceImpl()
{
    seService_ = std::make_shared<SEService>();
}

int32_t SEServiceImpl::CreateService()
{
    if (seService_ == nullptr) {
        ErrorLog("SEServiceImpl instance is nullptr");
        return CJ_INVALID_VALUE;
    }
    state_ = ServiceState::DISCONNECTED;
    bool initStatus = false;
    std::function<void()> callback = []() { InfoLog("cangjie api not support on/off callback now"); };
    int32_t statusCode = seService_->InitSEService(initStatus, callback);
    if (!initStatus || statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEServiceImpl: InitSEService falied");
        return CJ_INVALID_VALUE;
    }
    state_ = ServiceState::CONNECTED;
    return NATIVE_SUCCESS;
}

std::vector<std::shared_ptr<Reader>> SEServiceImpl::GetReaders(int32_t *errorCode)
{
    std::vector<std::shared_ptr<Reader>> readerList;
    if (seService_ == nullptr) {
        ErrorLog("SEServiceImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return readerList;
    }
    int32_t statusCode = seService_->GetReaders(readerList);
    if (statusCode != ErrorCode::SE_SUCCESS || readerList.empty()) {
        ErrorLog("SEServiceImpl::GetReaders");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return std::vector<std::shared_ptr<Reader>>();
    }
    return readerList;
}

bool SEServiceImpl::IsConnected(int32_t *errorCode)
{
    bool res = false;
    if (seService_ == nullptr) {
        ErrorLog("SEServiceImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return res;
    }
    int32_t statusCode = seService_->IsConnected(res);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEServiceImpl: IsConnected failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return false;
    }
    return res;
}

int32_t SEServiceImpl::Shutdown()
{
    if (seService_ == nullptr) {
        ErrorLog("SEServiceImpl instance is nullptr");
        return CJ_INVALID_VALUE;
    }
    int32_t statusCode = seService_->Shutdown();
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEServiceImpl: Shutdown failed");
        return CJ_INVALID_VALUE;
    }
    return NATIVE_SUCCESS;
}

std::string SEServiceImpl::GetVersion(int32_t *errorCode)
{
    std::string version = "";
    if (seService_ == nullptr) {
        ErrorLog("SEServiceImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return version;
    }
    int32_t statusCode = seService_->GetVersion(version);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEServiceImpl: GetVersion failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return "";
    }
    return version;
}
}  // namespace OMAPI
}  // namespace SE
}  // namespace OHOS